package com.portalEsporteClubeBahia.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.portalEsporteClubeBahia.app.ApplicationException;
import com.portalEsporteClubeBahia.app.bo.ChampionshipBO;
import com.portalEsporteClubeBahia.app.bo.MatchBO;
import com.portalEsporteClubeBahia.app.bo.MediaBO;
import com.portalEsporteClubeBahia.app.bo.NewsBO;
import com.portalEsporteClubeBahia.app.bo.StadiumBO;
import com.portalEsporteClubeBahia.app.bo.TeamBO;
import com.portalEsporteClubeBahia.app.vo.ChampionshipVO;
import com.portalEsporteClubeBahia.app.vo.MatchVO;
import com.portalEsporteClubeBahia.app.vo.MediaVO;
import com.portalEsporteClubeBahia.app.vo.NewsVO;
import com.portalEsporteClubeBahia.app.vo.StadiumVO;
import com.portalEsporteClubeBahia.app.vo.TeamVO;
import com.portalEsporteClubeBahia.app.vo.TypeMediaVO;

public class FindAllDataAC extends HttpServlet {

	private static final long serialVersionUID = -3505345782679733446L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		JSONObject returning = new JSONObject();
		String status = "";
 		String message = "";
		JSONArray arrayData = new JSONArray();
		
		try {
			
			List<NewsVO> list = new NewsBO().findAll();
			JSONArray arrayNews = new JSONArray();
			
			if(list!=null){
				for(NewsVO news : list){
					JSONObject jsonObject = new JSONObject();
					jsonObject.put("id", news.getId());
					jsonObject.put("typeID", news.getTypeNews().getId());
					jsonObject.put("typeName", news.getTypeNews().getName());
					jsonObject.put("title", news.getTitle());
					jsonObject.put("date", news.getDateFormat());
					jsonObject.put("urlImage", news.getUrlImage());
					jsonObject.put("content", news.getContent());
					jsonObject.put("shortContenc", news.getShortContent());
					jsonObject.put("urlWebSite", news.getUrlWebSite());
					jsonObject.put("ranking", news.getRanking());
					
					arrayNews.add(jsonObject);
				}
			}
			
			List<TypeMediaVO> listType = new MediaBO().findAllTypeMedia();
			JSONArray arrayTypeMedia = new JSONArray();
			
			if(listType.size()>0){
				
				for(TypeMediaVO type : listType){
					JSONObject jsonObject = new JSONObject();
					jsonObject.put("id", type.getId());
					jsonObject.put("name", type.getName());
					arrayTypeMedia.add(jsonObject);
				}
				
			}
			
			List<MediaVO> listMedia = new MediaBO().findAll();
			JSONArray arrayMedia = new JSONArray();
			
			if(list.size()>0){
				
				for(MediaVO media : listMedia){
					JSONObject jsonObject = new JSONObject();
					jsonObject.put("id", media.getId());
					jsonObject.put("typeID", media.getTypeMedia().getId());
					jsonObject.put("typeName", media.getTypeMedia().getName());
					jsonObject.put("title", media.getTitle());
					jsonObject.put("urlMedia", media.getUrlMedia());
					jsonObject.put("date", media.getDateFormat());
					jsonObject.put("shortContent", media.getShortContent());
					jsonObject.put("active", media.getActive());
					jsonObject.put("time", media.getTimeFormat());
					jsonObject.put("ranking", media.getRanking());
					arrayMedia.add(jsonObject);
				}
				
			}
			
			List<StadiumVO> listStadium = new StadiumBO().findAll();
			JSONArray arrayStadium = new JSONArray();
			
			if(listType.size()>0){
				
				for(StadiumVO stadium : listStadium){
					JSONObject jsonObject = new JSONObject();
					jsonObject.put("id", stadium.getId());
					jsonObject.put("name", stadium.getName());
					arrayStadium.add(jsonObject);
				}
				
			}
			
			List<TeamVO> listTeams = new TeamBO().findAll();
			JSONArray arrayTeams = new JSONArray();
			
			if(listType.size()>0){
				
				for(TeamVO team : listTeams){
					JSONObject jsonObject = new JSONObject();
					jsonObject.put("id", team.getId());
					jsonObject.put("name", team.getName());
					jsonObject.put("shortName", team.getShortName());
					jsonObject.put("urlImage", team.getUrlImage());
					arrayTeams.add(jsonObject);
				}
				
			}
			
			List<ChampionshipVO> listChamp = new ChampionshipBO().findAll();
			JSONArray arrayChamp = new JSONArray();
			
			if(listType.size()>0){
				
				for(ChampionshipVO champ : listChamp){
					JSONObject jsonObject = new JSONObject();
					jsonObject.put("id", champ.getId());
					jsonObject.put("name", champ.getName());
					jsonObject.put("years", champ.getYear());
					arrayChamp.add(jsonObject);
				}
				
			}
			
			List<MatchVO> listMatch = new MatchBO().findAll();
			JSONArray arrayMatch = new JSONArray();
			
			if(list!=null){
				for(MatchVO match : listMatch){
					JSONObject jsonObject = new JSONObject();
					jsonObject.put("id", match.getId());
					jsonObject.put("championshipID", match.getChampionship().getId());
					jsonObject.put("championshipName", match.getChampionship().getName());
					jsonObject.put("stadiumID",match.getStadium().getId());
					jsonObject.put("stadimName", match.getStadium().getName());
					jsonObject.put("awayTeamId", match.getAwayTeam().getId());
					jsonObject.put("awayTeamName", match.getAwayTeam().getName());
					jsonObject.put("awayTeamShortName", match.getAwayTeam().getShortName());
					jsonObject.put("awayTeamUrlImage", match.getAwayTeam().getUrlImage());
					jsonObject.put("homeTeamID", match.getHomeTeam().getId());
					jsonObject.put("homeTeamName", match.getHomeTeam().getName());
					jsonObject.put("homeTeamShortName", match.getHomeTeam().getShortName());
					jsonObject.put("homeTeamUrlImage", match.getHomeTeam().getUrlImage());
					jsonObject.put("date", match.getDateFormat());
					jsonObject.put("dateUTC", match.getDateUTCFormat());
					jsonObject.put("homeGoal", match.getHomeGoals());
					jsonObject.put("awayGoal", match.getAwayGoals());
					jsonObject.put("finished", match.getFinished());
					jsonObject.put("active", match.getActive());
					arrayMatch.add(jsonObject);
				}
			}
			
			status =  "OK";
 			message = "";
 			
 			arrayData.add(arrayNews);
 			arrayData.add(arrayTypeMedia);
 			arrayData.add(arrayMedia);
 			arrayData.add(arrayStadium);
 			arrayData.add(arrayTeams);
 			arrayData.add(arrayChamp);
 			arrayData.add(arrayMatch);
			
		}catch (ApplicationException e) {
			status  =  "ERROR";
  			message = e.getMessage();
		}
		
		returning.put("status",status);
 		returning.put("message",message);
 		returning.put("data",arrayData);
		
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(returning.toString());
	}

}
